package com.zm.controller;

import com.zm.bean.CodeEnum;
import com.zm.bean.ResultBean;
import com.zm.bean.TestDoorResultBean;
import com.zm.service.CabTestDoorResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping(value = "/cabTestDoorResult", produces = {"application/json;charset=UTF-8"})
public class CabTestDoorResultController {

    @Autowired
    private CabTestDoorResultService cabTestDoorResultService;

    //增、删、改、查
    @RequestMapping(value = "/saveOne", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean saveOne(@RequestBody TestDoorResultBean testDoorResultBean) {

        if (testDoorResultBean == null) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "测试结果不能为空", null);
        }

        TestDoorResultBean tBean = cabTestDoorResultService.save(testDoorResultBean);
        if (tBean == null) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "数据插入失败", null);
        }

        return new ResultBean(CodeEnum.SUCCESS.getCode(), "仓门测试结果上传成功", tBean);
    }

    @RequestMapping(value = "/saveList", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean saveList(@RequestBody List<TestDoorResultBean> testDoorResultBeans) {

        if (testDoorResultBeans == null || testDoorResultBeans.size() == 0) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "测试结果不能为空", null);
        }

        List<TestDoorResultBean> testDoorResultBeans1 = cabTestDoorResultService.saveAll(testDoorResultBeans);
        if (testDoorResultBeans1 == null) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "数据插入失败", null);
        }

        return new ResultBean(CodeEnum.SUCCESS.getCode(), testDoorResultBeans1.size() + "条仓门测试结果上传成功", testDoorResultBeans1);
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean deleteById(@RequestBody long id) {

        boolean isExist = cabTestDoorResultService.existsById(id);
        if (isExist) {
            try {
                cabTestDoorResultService.deleteById(id);
                return new ResultBean(CodeEnum.SUCCESS.getCode(), "数据删除成功", null);
            } catch (Exception e) {
                return new ResultBean(CodeEnum.FAIL.getCode(), "数据删除失败", e.getMessage());
            }

        } else {
            return new ResultBean(CodeEnum.FAIL.getCode(), "数据不存在", null);
        }

    }


    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public ResultBean getById(@RequestParam(value = "id", defaultValue = "-1") long id) {

        if (id == -1) {
            return new ResultBean(CodeEnum.FAIL.getCode(), "请输入正确的id号", null);
        }

        Optional<TestDoorResultBean> optionalBean = cabTestDoorResultService.findById(id);
        if (optionalBean.isPresent()) {
            TestDoorResultBean testDoorRecordBean = optionalBean.get();
            return new ResultBean(CodeEnum.SUCCESS.getCode(), "查询成功", testDoorRecordBean);
        }

        return new ResultBean(CodeEnum.FAIL.getCode(), "未查询到数据", null);

//        try {
//            TestDoorResultBean testDoorRecordBean = optionalBean.get();
//            return new ResultBean(CodeEnum.SUCCESS.getCode(), "查询成功", testDoorRecordBean);
//        } catch (NoSuchElementException noSuchElementException) {
//            return new ResultBean(CodeEnum.FAIL.getCode(), "未查询到数据", null);
//        }

    }

    @RequestMapping(value = "/getTestDoorResultList", method = RequestMethod.GET)
    @ResponseBody
    public ResultBean getTestDoorResultList() {

        List<TestDoorResultBean> testDoorRecordList = cabTestDoorResultService.findAll();
        if (testDoorRecordList != null) {
            return new ResultBean(CodeEnum.SUCCESS.getCode(), "查询成功", testDoorRecordList);
        }

        return new ResultBean(CodeEnum.FAIL.getCode(), "未查询到数据", null);
    }

    SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");

    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String showTestDoorResult(ModelMap modelMap, @RequestParam(required = false) String cid,
                                     @RequestParam(required = false) String dateTime) {
        List<TestDoorResultBean> testDoorResultList;

        if (dateTime == null) {
            dateTime = "1970-01-01";
        }

        Date date = new Date();
        try {
            date = ymdFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long d1 = date.getTime();
        long d2 = d1 + (24 * 60 * 60 * 1000);

        System.out.println("d1=" + d1);
        System.out.println("d2=" + d2);


//        SimpleDateFormat sdf =ne
        if (cid == null) {
            modelMap.addAttribute("cabinetId", "全部");
            testDoorResultList = cabTestDoorResultService.findAllByTestStartTimeAfter(d1);
        } else {
            modelMap.addAttribute("cabinetId", cid);
            testDoorResultList = cabTestDoorResultService.findTestDoorResultBeansByCabinetIdAndTestStartTimeAfter(cid, d1);
        }


        modelMap.addAttribute("resultBeanList", testDoorResultList);

        return "testDoorResult";
    }

    @RequestMapping(value = "/showDetail", method = RequestMethod.GET)
    public String showDetail(ModelMap modelMap, @RequestParam(required = false) String cid,
                                     @RequestParam(required = false) String dateTime) {
        List<TestDoorResultBean> testDoorResultList;

        if (dateTime == null) {
            dateTime = "1970-01-01";
        }

        Date date = new Date();
        try {
            date = ymdFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long d1 = date.getTime();
        long d2 = d1 + (24 * 60 * 60 * 1000);


        if (cid == null) {
            modelMap.addAttribute("cabinetId", "全部");
            testDoorResultList = cabTestDoorResultService.findAllByTestStartTimeAfter(d1);
        } else {
            modelMap.addAttribute("cabinetId", cid);
            testDoorResultList = cabTestDoorResultService.findTestDoorResultBeansByCabinetIdAndTestStartTimeAfter(cid, d1);
        }


        modelMap.addAttribute("resultBeanList", testDoorResultList);

        return "testDoorResult";
    }

}
